﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef ActionManager_h__
#define ActionManager_h__

#include "../utility/Singleton.h"
#include "../utility/NonCopyable.h"
#include "../utility/MacroTool.h"
#include "../utility/SmartPtrDef.h"

#include <unordered_map>
#include <memory>
#include <string>


namespace SkyEngine2d
{
	class Node;
	class Action;
	class IActableObject;
	struct ActionElement;
	class GameWorld;

	/*!
	 \brief 动作管理器
	 \details
	*/
	class SGE_API ActionManager
		:public Singleton<ActionManager, false>
	{
		FRIEND_SMART_PTR(ActionManager);
	public:
		//!析构
		~ActionManager();

		//! 添加一个动画
		/*!
		\param ActionSharedPtr action 动画的指针
		\param const std::shared_ptr<IActableObject> & target 动画的执行目标
		\param bool paused 添加后是否挂起
		\return bool  返回 \a false 则该目标不支持该动画
		*/
		bool addAction(ActionSharedPtr action, IActableObject* target, bool suspend = false);

		//! 挂起某个执行目标
		/*!
		\param const IActableObject * target 执行目标
		\return void
		*/
		void suspendActionOfTarget(const IActableObject* target);

		//! 恢复某个执行目标
		/*!
		\param const IActableObject * target 执行目标
		\return void
		*/
		void resumeActionOfTarget(const IActableObject* target);

		//! 挂起某个动画
		/*!
		\param const IActableObject * target 动画的执行目标
		\param const std::wstring & name 动画的名字
		\return void
		*/
		void suspendAction(const IActableObject* target, const std::wstring& name);

		//! 恢复某个动画
		/*!
		\param const IActableObject * target 动画的执行目标
		\param const std::wstring & name 动画的名字
		\return void
		*/
		void resumeAction(const IActableObject* target, const std::wstring& name);

		//! 移除所有动画
		/*!
		\return void
		*/
		void removeAllActions();

		//! 移除该执行目标的所有动画
		/*!
		\param const IActableObject * target 动画执行目标
		\return void
		*/
		void removeAllActionsOfTarget(const IActableObject* target);

		//! 移除执行目标的某个动画
		/*!
		\param const std::wstring & name 动画的名字
		\param const IActableObject * target 执行目标
		\return void
		*/
		void removeActionByName(const std::wstring& name, const IActableObject* target);

		//! 获取执行目标中的某个动画 
		/*!
		\param const std::wstring & name 动画名字
		\param const IActableObject * target 执行目标
		\return std::shared_ptr<Action>
		*/
		std::shared_ptr<Action> getActionByName(const std::wstring& name, const IActableObject* target);

		//! 更新动画
		/*!
		\param float dt 时间增量
		\return void
		*/
		void update(float dt);

	protected:
		//! 构造
		ActionManager();

		friend class GameWorld;
	public:
		//存放所有的节点的动画信息
		//以节点的指针作为key   这个节点的全部动画信息作为value
		std::unordered_map<const IActableObject*, std::unique_ptr<ActionElement>>  m_actionTable;
		//被删除的节点的记录
		std::vector<const IActableObject*>     m_removedNode;
		//当前正在执行的动画信息
		ActionElement* m_currActionElement;
		//是否全部清空
		bool m_allRemoved;
	};
}

#endif // ActionManager_h__
